
import json
import time
import request
import flask
from flask_cors import *
import mysql.connector
# MYSQL操作参考：https://www.runoob.com/python3/python-mysql-connector.html
# from lxml.html import etree
# from selenium import webdriver
from cls.orm.main import PG
from flask import jsonify
from app.emergency.stat import Statistic

# import logging
# log = logging.getLogger('werkzeug')
# log.setLevel(logging.ERROR)

app = flask.Flask(__name__)
CORS(app, supports_credentials=True)
app.config['JSON_AS_ASCII'] = False
# app = flask.Flask(__name__, static_folder='', static_url_path='')


@app.route('/ls/<e>/<r>')
def testFun(e,r):
    return '你输入的参数是：' + e + r


@app.route('/guo/<e>/<r>')
def testFun2(e,r):
    return 'testfun'


@app.route('/geom2geojson/<table>/<where>')
def toGeojson(table,  where=None):
    db = PG()
    geojsonStr = db.getGeomJson(table, where)
    propRes = db.getProperties(table, where)
    feaList = []
    tag = 0
    resDict = geojsonStr[tag][0]
    length = len(geojsonStr)
    while tag < length:
        feature = geojsonStr[tag][0]['features']
        feature['properties'] = propRes[tag][0]
        feaList.append(feature)
        tag = tag + 1
    resDict['features'] = feaList
    return jsonify(resDict)


@app.route('/geom2geojson/<table>')
def allGeojson(table):
   return toGeojson(table)

@app.route('/testmysql')
def getData():
    # 打开数据库连接
    # db = MySQLdb.connect("120.79.50.98", "wdkj@dev", "wdkj@devpwd", "yjgl", charset='utf8')
    db = mysql.connector.connect(
        host="120.79.50.98",
        user="wdkj@dev",
        passwd="wdkj@devpwd",
        database="yjgl"
    )
    mycursor = db.cursor()

    mycursor.execute("SHOW TABLES")
    rStr = ''
    # for x in mycursor:
    #     return print(x)
        # rStr = rStr + x
    db.close()
    return 'hello'

    # 关闭数据库连接


@app.route('/getpost/<arg1>', methods=['GET', 'POST'])
def testgp(arg1):
    # args = request.json.get("arg1")
    args_3 = request.json['arg1']
    return arg1


@app.route('/api', methods=['get', 'POST'])
def toapi():
    return 'apis'


# toGeojson()
#
def getPage():
    pass
    # opts = webdriver.ChromeOptions()
    # # 把chrome设置成无界面模式，不论windows还是linux都可以，自动适配对应参数
    # opts.headless = False
    #
    # driver = webdriver.Chrome(options=opts)
    # driver.get("https://www.baidu.com")
    # time.sleep(10000)



if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)

